//

import UIKit

class WP_CustomerAddViewController: WP_BasicViewController {
    
    var namestr:String!       //name
    var phonestr:String!      //电话
    var addressstr:String!    //地址
    var companystr:String!    //公司
    var remarkstr:String!     //备注
    var model:CustomerModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavTitle("CustomerAdd")
        // Do any additional setup after loading the view.
        
        self.view.addSubview(cTableView)
        setFootView()
        
         WP_CoreDataModel.coreDataManager.setEntityName(text: "CustomerModel")
    }
    
    lazy var cTableView:UITableView = {
        
        let tableview = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight - kNavBarHeight), style: .plain)
        tableview.delegate = self
        tableview.dataSource = self
        tableview.rowHeight = kRatioHeight(100)
        tableview.tableFooterView = UIView()
        tableview .register(WP_FieldTableViewCell.classForCoder(), forCellReuseIdentifier:NSStringFromClass(WP_FieldTableViewCell.classForCoder()))
        tableview.separatorColor = UIColor.white
        return tableview
        
    }()
    //footview
    func setFootView()  {
        
        let foot = WP_FootView(frame: CGRect(x: 0, y: kScreenHeight-kTabBarHeight, width: kScreenWidth, height: kTabBarHeight))
        self.view.addSubview(foot)
        weak var weakSelf = self
        foot.FootBlock = {
            
            if weakSelf!.namestr?.count == 0 ||  weakSelf!.phonestr?.count == 0 || weakSelf!.companystr?.count == 0 || weakSelf!.addressstr?.count == 0{
                return
            }
            var dic = Dictionary<String,Any>()
            //            let dic2 = Dictionary<String,Any>()
            //
            dic.updateValue(weakSelf!.namestr ?? "", forKey: "name")
            dic.updateValue(weakSelf!.phonestr ?? "", forKey: "phone")
            dic.updateValue(weakSelf!.companystr ?? "", forKey: "company")
            dic.updateValue(weakSelf!.addressstr ?? "", forKey: "address")
            dic.updateValue(weakSelf!.remarkstr ?? "", forKey: "remark")
            
            if weakSelf!.model == nil {//添加
                
                dic.updateValue(NSNumber.init(value: Int(weakSelf!.randomNum())!), forKey: "iD")
                
                 WP_CoreDataModel.coreDataManager.saveData(parameter: dic, success: { weakSelf?.navigationController?.popViewController(animated: true)}, fail: {})
                
            }else {//修改
                
                WP_CoreDataModel.coreDataManager.updateData(parameter: dic, key: "iD=\(weakSelf!.model?.iD ?? 0)", success: { weakSelf?.navigationController?.popViewController(animated: true)}, fail: {})
            }
        }
    }
    
}


extension WP_CustomerAddViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:WP_FieldTableViewCell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(WP_FieldTableViewCell.classForCoder()), for: indexPath) as! WP_FieldTableViewCell
        cell.index = indexPath.row
        cell.selectionStyle = .none
        cell.delegate = self;
        if indexPath.row == 0 {
            cell.titleLabel.text = "Name"
            cell.tfield.placeholder = "Input a Name"
            cell.tfield.text = model?.name
            namestr = model?.name ?? ""
            
        }else if indexPath.row == 1 {
            cell.titleLabel.text = "Phone"
            cell.tfield.placeholder = "Input a Phone"
            cell.tfield.text = model?.phone
            phonestr = model?.phone ?? ""
            
        }else if indexPath.row == 2 {
            cell.titleLabel.text = "Company"
            cell.tfield.placeholder = "Input a Company"
            cell.tfield.text = model?.company
            companystr = model?.company ?? ""
            
        }else if indexPath.row == 3 {
            cell.titleLabel.text = "Address"
            cell.tfield.placeholder = "Input a Address"
            cell.tfield.text = model?.address
            addressstr = model?.address ?? ""
            
        }else if indexPath.row == 4 {
            cell.titleLabel.text = "Remarks"
            cell.tfield.placeholder = "Input a Remarks"
            cell.tfield.text = model?.remark
            remarkstr = model?.remark ?? ""
        }
        
        return cell
    }
    
}



extension WP_CustomerAddViewController:WP_FieldTableViewDelegate {
    
    func SendIndexCell(index: NSInteger, obj: String) {
        switch index {
        case 0:
            namestr = obj
        case 1:
            phonestr = obj
        case 2:
            companystr = obj
        case 3:
            addressstr = obj
        case 4:
            remarkstr = obj
        default: break
            
        }
    }
}



